package com.starblink.home.subscription

import android.content.res.ColorStateList
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.postDelayed
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.starblink.android.basic.base.EmptyViewModel
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.ext.bindFragment
import com.starblink.android.basic.ext.bindTab
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.drawable
import com.starblink.android.basic.ext.toastWarningL
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.route.RoutePage
import com.starblink.basic.storage.SkStorage
import com.starblink.home.R

import com.starblink.home.databinding.FragmentHomeSubscriptionBinding
import com.starblink.home.databinding.TabItemHomeSubsBinding
import com.starblink.home.subscription.child.SubsChildFragment
import com.starblink.home.subscription.guide.SubsGuidePop
import com.starblink.library.widget.subscription.showSubscribeDialog

/**
 *  Create by yuanl at 2023/9/28
 **/
class HomeSubscriptionFragment :
    BaseTVMFragment<FragmentHomeSubscriptionBinding, EmptyViewModel>(), ViewPager.OnPageChangeListener {


    companion object {

        const val GUIDE_CACHE_KEY = "home_subs_guide"

        @JvmStatic
        fun newIns() = HomeSubscriptionFragment().apply {

        }

    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeSubscriptionBinding =
        FragmentHomeSubscriptionBinding.inflate(inflater, container, false)

    override fun spmPageId(): Int = SpmPageDef.PAGE_10235

    override fun isSpmTrackWithLifecycle(): Boolean = true

    var tabIndex = 0

    val fragments = mutableListOf<SubsChildFragment>()

    override fun initView(view: View) {
        super.initView(view)

        viewBinding.btnAdd.trackData(spmPageId(), SpmElementDef.ELE_33081, onlyHit = true)
        viewBinding.btnAdd.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_SEARCH)
        }
        viewBinding.btnSetting.setOnClickListener {
            requireActivity().showSubscribeDialog()
        }
        initTabs()
    }

    override fun startObserve() {

    }

    override fun initData() {

    }

    private fun initTabs() {
        val tabNames = resources.getStringArray(R.array.subs_tabs).toList()
        val tabIcons = arrayOf(
            R.drawable.ic_svg_subs_tab_all,
            R.drawable.ic_svg_subs_tab_brands,
            R.drawable.ic_svg_subs_tab_stores,
            R.drawable.ic_svg_subs_tab_items
        )
        viewBinding.vIndicator.bindTab(
            tabNames.size,
            onTabSelect = { i, b ->
                b.vTab.setBackgroundColor(color(com.starblink.basic.style.R.color.gu_bg_dark1))
                b.vTab.setTextColor(color(com.starblink.basic.style.R.color.white))
                b.vTab.iconTint =
                    ColorStateList.valueOf(color(com.starblink.basic.style.R.color.white))
            },
            onDeselected = { i, b ->
                b.vTab.setBackgroundColor(color(com.starblink.basic.style.R.color.gu_bg_light1))
                b.vTab.setTextColor(color(com.starblink.basic.style.R.color.gu_text_secondary))
                b.vTab.iconTint =
                    ColorStateList.valueOf(color(com.starblink.basic.style.R.color.gu_text_secondary))
            },
            gap = 8.dp, edgeGap = 12.dp,
            viewPager = viewBinding.vp,
        ) {
            TabItemHomeSubsBinding.inflate(layoutInflater).apply {
                vTab.text = tabNames[it]
                vTab.icon = drawable(tabIcons[it])
                vTab.setBackgroundColor(color(if (it == tabIndex) com.starblink.basic.style.R.color.gu_bg_dark1 else com.starblink.basic.style.R.color.gu_bg_light1))
                vTab.setTextColor(color(if (it == tabIndex) com.starblink.basic.style.R.color.white else com.starblink.basic.style.R.color.gu_text_secondary))
                vTab.iconTint =
                    ColorStateList.valueOf(color(if (it == tabIndex) com.starblink.basic.style.R.color.white else com.starblink.basic.style.R.color.gu_text_secondary))
                root.trackData(
                    spmPageId(),
                    SpmElementDef.ELE_30060,
                    params = mapOf(
                        "tabName" to tabNames[it],
                    ),
                    onlyHit = true,
                )
            }
        }
        viewBinding.vp.bindFragment(childFragmentManager, count = 4, pageChangeListener = this) {
            val f = if (it == 0) {
                SubsChildFragment.newIns(null)
            } else {
                SubsChildFragment.newIns(it)
            }
            fragments.add(f)
            f
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            showGuidePop()
        }

        if (hasBindingInit() && !hidden) {
            fragments.getOrNull(viewBinding.vp.currentItem)?.onTabCheck()
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            showGuidePop()
        }

        if (hasBindingInit() && isVisibleToUser) {
            fragments.getOrNull(viewBinding.vp.currentItem)?.let {
                it.userVisibleHint = isVisibleToUser
                it.onTabCheck()
            }
        }
    }

    private fun showGuidePop() {
        if (!SkStorage.getBoolean(GUIDE_CACHE_KEY, false)) {
            viewBinding.btnAdd.postDelayed(150L) {
                SubsGuidePop(requireContext()).showPopupWindow(viewBinding.btnAdd)
                SkStorage.put(GUIDE_CACHE_KEY, true)
            }
        }
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        fragments.getOrNull(position)?.onTabCheck()
    }

    override fun onPageScrollStateChanged(state: Int) {

    }
}